//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.bs.pub.action;

import java.util.HashMap;
import java.util.Map;

import nc.bs.framework.common.NCLocator;
import nc.bs.pubapp.pf.action.AbstractPfAction;
import nc.bs.to.m5f.plugin.ActionPlugInPoint;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.impl.to.m5f.action.ApproveAction;
import nc.impl.to.m5f.action.DoFinanceAction;
import nc.impl.to.m5f.action.rule.approve.CheckEnableApproveRule;
import nc.pub.templet.converter.util.helper.ExceptionUtils;
import nc.uapbd.vo.sever.ListenerSever;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pubapp.pattern.model.transfer.bill.ServerBillToClient;
import nc.vo.to.m5f.entity.SettleListVO;
import nc.vo.to.pub.para.ParaUtilsForTo;

public class N_5F_APPROVE extends AbstractPfAction<SettleListVO> {

    private SettleListVO[] doAutoTranFiAfterApprove(SettleListVO[] vos) {
        Map<Integer, SettleListVO> mapIndex = new HashMap();
        Map<String, UFBoolean> mapCache = new HashMap();
        DoFinanceAction action = new DoFinanceAction();
        int len = vos.length;

        int i;
        SettleListVO vo;
        String pk_org;
        UFBoolean bAutoDoFinance;
        for(i = 0; i < len; ++i) {
            vo = vos[i];

            pk_org = vo.getParentVO().getPk_org();
            bAutoDoFinance = UFBoolean.FALSE;
            if (null != mapCache.get(pk_org)) {
                bAutoDoFinance = (UFBoolean)mapCache.get(pk_org);
            } else {
                bAutoDoFinance = ParaUtilsForTo.getInstance().getTO15(pk_org);
            }

            if (null != bAutoDoFinance && bAutoDoFinance.booleanValue()) {
                mapIndex.put(i, vo);
            }
        }

        if (mapIndex.size() > 0) {
            for(i = 0; i < len; ++i) {
                vo = vos[i];
                pk_org = vo.getParentVO().getPk_org();
                bAutoDoFinance = (UFBoolean)mapCache.get(pk_org);
                if (null != bAutoDoFinance && bAutoDoFinance.booleanValue()) {
                    bAutoDoFinance = (UFBoolean)mapCache.get(pk_org);
                }

                vos[i] = action.doFinance(new SettleListVO[]{vo})[0];
            }
        }

        return vos;
    }

    protected CompareAroundProcesser<SettleListVO> getCompareAroundProcesserWithRules(Object userObj) {
        CompareAroundProcesser<SettleListVO> processor = new CompareAroundProcesser(ActionPlugInPoint.N_5F_APPROVE);
        IRule<SettleListVO> rule = new CheckEnableApproveRule();
        processor.addBeforeRule(rule);
        return processor;
    }

    protected SettleListVO[] processBP(Object userObj, SettleListVO[] clientFullVOs, SettleListVO[] originBills) {
        SettleListVO[] bills = (new ApproveAction()).approve(clientFullVOs, originBills);
        SettleListVO[] newbills = this.doAutoTranFiAfterApprove(bills);
        ServerBillToClient<SettleListVO> clientTransfer = new ServerBillToClient();
        SettleListVO[] vos = (SettleListVO[])clientTransfer.construct(originBills, newbills);
//        for (SettleListVO vo:
//        vos) {
//            try {
//                NCLocator.getInstance().lookup(ListenerSever.class).settleListener(new SettleListVO[]{vo});
//            } catch (BusinessException e) {
//                e.printStackTrace();
//                nc.vo.pubapp.pattern.exception.ExceptionUtils.wrappBusinessException(e.getMessage());
//            }
//        }

        return vos;
    }
}
